package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //添加购物车
    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入的商品是否在购物车中
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        Long currentId = BaseContext.getCurrentId();
        cart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(cart);

        //如果存在，则增加数量
        if (list != null && list.size() > 0) {
            ShoppingCart cart1 = list.get(0);
            cart1.setNumber(cart1.getNumber() + 1);
            shoppingCartMapper.updateNumber(cart1);
        } else {
            //否则，添加到购物车，数量默认为1
            //判断当前添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //添加的是菜品(为购物车赋值)
                Dish dish = dishMapper.getById(dishId);
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());

            } else {
                //添加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(cart);
        }
    }

    //查看购物车
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long UserId = BaseContext.getCurrentId();
        ShoppingCart cart = ShoppingCart.builder().userId(UserId).build();
        return shoppingCartMapper.list(cart);
    }

    //清空购物车
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);

    }

    //删除购物车商品
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        Long UserId = BaseContext.getCurrentId();
        cart.setUserId(UserId);
        List<ShoppingCart> list = shoppingCartMapper.list(cart);


        if (list != null && !list.isEmpty()) {
            ShoppingCart cartInDB = list.get(0);
            Integer number = cartInDB.getNumber();
            if (number <= 1) {
                // 当数量小于等于1时，直接删除整条记录
                shoppingCartMapper.delete(cartInDB);
            } else {
                // 当数量大于1时，减少数量
                cartInDB.setNumber(number - 1);
                shoppingCartMapper.updateNumber(cartInDB);
            }
        }


    }
}
